package com.br.telas.adapters;

import java.util.ArrayList;
import java.util.Iterator;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.br.main.R;
import com.br.objetos.BarMobile;
import com.br.objetos.GrupoMobile;
import com.br.utilidades.Utilidades;

public class BarAdapter extends BaseAdapter {

	private BarMobile[] bares;
	private Context ctx;
	
	public BarAdapter(ArrayList<BarMobile> arrayBares, Context ctx)
	{
		this.ctx = ctx;
		
		bares = new BarMobile[arrayBares.size()];
		int aux = 0;
		Iterator<BarMobile> it = arrayBares.iterator();  
		while (it.hasNext()) {  
		    BarMobile b = (BarMobile) it.next();
		    bares[aux] = b;
		    aux++;
		}
	}
	
	@Override
	public int getCount() {
		return bares.length;
	}

	@Override
	public Object getItem(int arg0) {
		if(arg0 >= bares.length)
		  return null;
		else
	      return bares[arg0];
	}

	@Override
	public long getItemId(int arg0) {
		BarMobile o = (BarMobile)getItem(arg0);// TODO Auto-generated method stub
		if(o == null)
			return 0;
		else
			return o.getIdBar();
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View aux;
		if(convertView == null)
		{
		   LayoutInflater inflater = (LayoutInflater)ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		   aux = inflater.inflate(R.layout.barlistitem, null);
		}
		else {
			aux = convertView;
		}
		
		BarMobile bar = (BarMobile)getItem(position);
		TextView nomeBar = (TextView)aux.findViewById(R.id.nomeBar);
		TextView enderecoBar = (TextView)aux.findViewById(R.id.enderecoBar);
		TextView distanciaBar = (TextView)aux.findViewById(R.id.distanciaBar);
		nomeBar.setText(bar.getRazaoSocial());
		enderecoBar.setText(bar.getEndereco());
		distanciaBar.setText("" + Utilidades.arredondarDistancia(bar.getMedia()) + " Km");
		aux.setTag(bar);
		
//		aux.setOnClickListener(new OnClickListener() {
//			
//			@Override
//			public void onClick(View v) {
//				Bar bar = (Bar)v.getTag();
//				Toast t = Toast.makeText(ctx, bar.razaoSocial+", "+bar.idBar, Toast.LENGTH_LONG);
//			}
//		});
		return aux;
	}
}
